package com.example.springboottools.holiday;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.example.springboottools.entiy.ExcelExportUtil;
import com.example.springboottools.holiday.domain.Data;
import com.example.springboottools.holiday.domain.JsonRootBean;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Holiday_2025 {
    @Test
    public void test() throws Exception {
        String year = "2025";
        List<Data> dataAllList = new ArrayList<>();
        for (int i = 1; i < 13; i++) {
            String url = "https://oneapi.coderbox.cn/openapi/public/holiday";
            if (i<10){
                url = url+"?date="+year+"-0"+i+"&queryType=2";
            }else {
                url = url+"?date="+year+"-"+i+"&queryType=2";
            }
            String s = HttpUtil.get(url);
            JsonRootBean jsonRootBean = JSON.parseObject(s, JsonRootBean.class);
            List<Data> data = jsonRootBean.getData();
            List<Data> collect = data.stream().filter(dataItem -> dataItem.getStatus() == 2&&dataItem.getStatutory()==1)
                    .collect(Collectors.toList());
            dataAllList.addAll(collect);
        }
        List<Map> holidayMapList=new ArrayList<>();
        dataAllList.forEach(item->{
            Map<String, Object> map=new HashMap<>();
            String date = item.getDate();
            String[] split = date.split("-");
            int month = Integer.parseInt(split[1]);
            int day = Integer.parseInt(split[2]);
            map.put("date",split[0]+"-"+month+"-"+day);
            map.put("year",split[0]);
            map.put("month", month);
            map.put("day", day);
            map.put("status",item.getStatus());
            holidayMapList.add(map);
        });
        String sssUrl="D:\\staples_VP\\day\\2025\\holiday_festival_2025.xls";
        excelImport2(holidayMapList,sssUrl);
    }
    @Test
    public void OAHolidayTest() throws Exception {
        String year = "2025";
        List<Data> dataAllList = new ArrayList<>();
        for (int i = 1; i < 13; i++) {
            String url = "https://oneapi.coderbox.cn/openapi/public/holiday";
            if (i<10){
                url = url+"?date="+year+"-0"+i+"&queryType=2";
            }else {
                url = url+"?date="+year+"-"+i+"&queryType=2";
            }
            String s = HttpUtil.get(url);
            JsonRootBean jsonRootBean = JSON.parseObject(s, JsonRootBean.class);
            List<Data> data = jsonRootBean.getData();
            List<Data> collect = data.stream().filter(dataItem -> dataItem.getStatus() == 2)
                    .collect(Collectors.toList());
            dataAllList.addAll(collect);
        }
        List<Map> holidayMapList=new ArrayList<>();
        dataAllList.forEach(item->{
            Map<String, Object> map=new HashMap<>();
            String date = item.getDate();
            String[] split = date.split("-");
            //int month = Integer.parseInt(split[1]);
            //int day = Integer.parseInt(split[2]);
            map.put("date",date);
            map.put("year",split[0]);
            map.put("month", split[1]);
            map.put("day", split[2]);
            map.put("status",item.getStatus());
            holidayMapList.add(map);
        });
        String sssUrl="D:\\staples_VP\\day\\2025\\holiday_oa_2025.xls";
        excelImport2(holidayMapList,sssUrl);
    }
    private void excelImport2(List<Map> mapListAll,String excelPath) throws IOException {
        ExcelExportUtil excelExportUtil=new ExcelExportUtil();
        String title="title";
        //各个列的表头
        String[] heardList={"日期","类型","年份","月份","天数"};
        //各个列的元素key值
        String[] heardKey={"date","dateType","year","month","day"};
        //需要填充的数据信息
        excelExportUtil.setTitle(title);
        excelExportUtil.setHeardList(heardList);
        excelExportUtil.setHeardKey(heardKey);
        excelExportUtil.setData(mapListAll);
        excelExportUtil.exportExportLocal(excelPath);
        System.out.println(JSON.toJSONString(mapListAll));
    }
}
